#!/bin/bash
#
#  Copyright (c) 2021, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

set -euxo pipefail

OT_CMAKE_NINJA_TARGET=${OT_CMAKE_NINJA_TARGET:-}

QORVO_PLATFORMS=(gp712 qpg6105 qpg6200 qpg7015m)
readonly QORVO_PLATFORMS

OT_SRCDIR="$(pwd)"
readonly OT_SRCDIR

OT_OPTIONS=(
    "-DCMAKE_BUILD_TYPE=MinSizeRel"
    "-DOT_PLATFORM=external"
    "-DOT_SLAAC=ON"
)
readonly OT_OPTIONS

die()
{
    echo " ** ERROR: $1"
    exit 1
}

build()
{
    builddir="${OT_CMAKE_BUILD_DIR:-build}"

    mkdir -p "${builddir}"
    cd "${builddir}"

    cmake -GNinja -DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"

    if [[ -n ${OT_CMAKE_NINJA_TARGET[*]} ]]; then
        ninja "${OT_CMAKE_NINJA_TARGET[@]}"
    else
        ninja
    fi

    cd "${OT_SRCDIR}"
}

main()
{
    if [[ $# == 0 ]]; then
        echo "Please specify a platform: ${QORVO_PLATFORMS[*]}"
        exit 1
    fi

    local platform="$1"
    echo "${QORVO_PLATFORMS[@]}" | grep -wq "${platform}" || die "Unsupported platform ${platform}"
    shift

    local options=("${OT_OPTIONS[@]}")
    local cmdline_options=("$@")

    case "${platform}" in
        gp712)
            OT_CMAKE_NINJA_TARGET=("ot-cli-ftd" "ot-cli-mtd")
            options+=("-DCMAKE_TOOLCHAIN_FILE=src/${platform}/arm-linux-gnueabihf.cmake")
            build -DQORVO_PLATFORM="${platform}" -DOT_THREAD_VERSION="1.1" "${options[@]}" "${cmdline_options[@]}"
            ;;
        qpg7015m)
            OT_CMAKE_NINJA_TARGET=("ot-rcp" "ot-cli-ftd" "ot-cli-mtd")
            options+=("-DCMAKE_TOOLCHAIN_FILE=src/${platform}/arm-linux-gnueabihf.cmake")
            build -DQORVO_PLATFORM="${platform}" "${options[@]}" "${cmdline_options[@]}"
            ;;
        qpg6105 | qpg6200)
            OT_CMAKE_NINJA_TARGET=("ot-cli-ftd" "ot-cli-mtd")
            options+=("-DCMAKE_TOOLCHAIN_FILE=src/${platform}/arm-none-eabi.cmake")
            build -DQORVO_PLATFORM="${platform}" "${options[@]}" "${cmdline_options[@]}"
            ;;
    esac

    case "${platform}" in
        qpg6105 | qpg6200)
            find "${builddir}/bin" -type f -executable -not -name '*.*' -not -name "${platform}-*" -not -name "*-test-*" \
                -execdir sh -c 'x="$1"; arm-none-eabi-objcopy -O ihex "$1" "$2-$(basename ${x}).hex"' _ {} "${platform}" \;
            ;;
    esac
    find "${builddir}/bin" -type f -executable -not -name '*.*' -not -name "${platform}-*" -not -name "*-test-*" \
        -execdir sh -c 'x="$1"; mv "$x" "$2-$(basename ${x}).elf"' _ {} "${platform}" \;
}

main "$@"
